package com.elite.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.springframework.jdbc.core.JdbcTemplate;

public class Util {
	private static final Log log=LogFactory.getLog(Util.class);
	public static String getERRO(Exception e){
		String str="";
		try{
            StringWriter sw=new StringWriter();  
            e.printStackTrace(new PrintWriter(sw,true));  
            str=sw.toString(); 	
		}catch(Exception e1){
			str=e.toString();
			log.error("getERRO:"+e1.toString());
		}
		return str;
	}
	public static Document getDoc()throws Exception{
		Document result=DocumentFactory.getInstance().createDocument("utf-8");
		return result;
	}
	public static String getString(Object obj){
		if(obj==null){
			return "";
		}else{
			return obj.toString().replace(" ","");
		}
	}
	public static String getQueryResult(String sql,DataSource dataSource) throws Exception{
		String returnxml="";
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		List<Map> list=jdbcTemplate.queryForList(sql);
		if(list!=null&&list.size()>0){
			Document result=Util.getDoc();
        	Element root=result.addElement("Root");
			for(int i=0;i<list.size();i++){
				Map map=list.get(i);
				Iterator iter=map.entrySet().iterator();
				Element QueryResult=root.addElement("QueryResult");
				while(iter.hasNext()){
					Map.Entry entry=(Map.Entry)iter.next();
					String Key=(String) entry.getKey();
					log.debug("Key:"+Key);
					String Value="";
					if(entry.getValue() instanceof BigDecimal){
						Value=entry.getValue().toString();
					}else if(entry.getValue() instanceof java.sql.Timestamp){
						Value=entry.getValue().toString();
					}else{
						Value=(String) entry.getValue();
					}
					log.debug("Value:"+Value);
					QueryResult.addElement(Key).addText(Util.getString(Value));
				}
					
			}
			returnxml=result.asXML();
		}
		return returnxml;
	}
	
	public static String getUpdateResult(String sql,DataSource dataSource)throws Exception{
		String returnxml="";
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		log.debug("sql:"+sql);
		int UpdateResult=jdbcTemplate.update(sql);
		log.debug("UpdateResult:"+UpdateResult);
		Document result=Util.getDoc();
    	Element root=result.addElement("Root");
    	root.addElement("UpdateResult").addText(Util.getString(UpdateResult));
    	returnxml=result.asXML();
		return returnxml;
	}
	public static int getCount(String sql,DataSource dataSource)throws Exception{
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		int result=jdbcTemplate.queryForInt(sql);
		return result;
	}
	
	public static String chaxunString(String sql,DataSource dataSource) throws Exception{
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String resultList=(String)jdbcTemplate.queryForObject(sql,java.lang.String.class);
        return resultList;
	}
	
	public static List chaxunList(String sql,DataSource dataSource) throws Exception{
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        List resultList=jdbcTemplate.queryForList(sql);
        return resultList;
	}
}
